package com.foreknow.demo14;

import com.foreknow.demo14.*;
//导入包


import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/*
 * 数据库连接
 */
public class Conn {
    static Connection  conn;
   public boolean  Connectiontest(){
       boolean isSuccess=false;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/basic5?useSSL=false&serverTimezone=UTC";
             conn = DriverManager.getConnection(url, "root", "123456");
          //  Statement stmt = conn.createStatement();
//            ResultSet rs = stmt.executeQuery("select * from  login");
//            while (rs.next()){
//                String username = rs.getString("username");
//                String password = rs.getString("password");
//            }
            if (conn != null) {
                isSuccess=true;
            } else {
                isSuccess=false;
            }

            //stmt.close();
//            conn.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isSuccess;
    }

    public boolean islogin(String username,String password){
       boolean isin=false;  //是否登入成功
        List<Login> list=new ArrayList<>();
        try {
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from login");
            while (rs.next()){
                String username1 = rs.getString("username");
                String password1 = rs.getString("password");
                Login login = new Login(username1, password1);
                list.add(login);
            }
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getUsername().equals(username)&&list.get(i).getPassword().equals(password)){
                    isin=true;

                }
            }
            rs.close();
            isin=false;
            stmt.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return isin;
    }
}